package com.newupbank.schedule.mail.task;

import com.newupbank.schedule.mail.dao.IMailDao;
import com.newupbank.schedule.mail.vo.MailConfigVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author: liminghui
 * @date: 2021/1/17 23:09
 * @version: 1.0
 * @description: 适合从数据库只读取配置一次
 */
// @Component
// @EnableScheduling
public class MailScheduleTask1 {
    private static final Logger logger = LoggerFactory.getLogger(MailScheduleTask1.class);
    @Autowired
    private SendMailMsg sendMailMsg;

    @Autowired
    private IMailDao mailDao;

    /*定时执行任务方法 每天5点20执行该任务*/
    // @Scheduled(cron ="0 20 17 * * *")
    @Scheduled(cron = "${mail.task.cron}")
    public void executeTask() {
        List<MailConfigVO> mailConfigVOList = mailDao.queryAllMailConfigVO();
        for (MailConfigVO mailConfigVO : mailConfigVOList) {

            String message = sendMailMsg.getOneMessage(mailConfigVO.getContentFromUrl());
            sendMailMsg.sendMessage(mailConfigVO, message);
        }
        logger.info("MailScheduleTask1--->executeTask is successful");
    }

}
